Imports System.Web.Security
Imports MIMS.WRS.Business
Imports MIMS.WRS.Data
Imports MIMS.WRS.Entities


Public Class ConPointSelection
    Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub
    Protected WithEvents Label1 As System.Web.UI.WebControls.Label
    Protected WithEvents lblErr As System.Web.UI.WebControls.Label

    Protected WithEvents txtUsername As System.Web.UI.WebControls.TextBox
    Protected WithEvents RequiredFieldValidator1 As System.Web.UI.WebControls.RequiredFieldValidator
    Protected WithEvents txtPassword As System.Web.UI.WebControls.TextBox
    Protected WithEvents btnOK As System.Web.UI.WebControls.Button
    Protected WithEvents lnkForgotYourPassword As System.Web.UI.WebControls.LinkButton
    Protected WithEvents ddlConPoint As System.Web.UI.WebControls.DropDownList

    'NOTE: The following placeholder declaration is required by the Web Form Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Put user code to initialize the page here
        If (Not Page.IsPostBack) Then
            Dim conPointString As String = ""
            conPointString = Convert.ToString(Request.Item("ConPointIDString"))
            If (conPointString Is Nothing OrElse conPointString = "") Then
                'Reload from DB
                Dim shoppingOrderCol As ShoppingOrderCollection = ShoppingListService.GetShoppingOrderByShoppingUserId(ShoppingConext.ShopingUserId)
                Dim conPointIDList As New ListItemCollection
                conPointString = ""
                If (Not shoppingOrderCol Is Nothing AndAlso shoppingOrderCol.Count > 0) Then
                    For Each item As ShoppingOrder In shoppingOrderCol
                        If (conPointIDList.FindByValue(item.ConPointID.ToString()) Is Nothing) Then
                            conPointIDList.Add(New ListItem(item.ConPointID.ToString(), item.ConPointID.ToString()))
                            conPointString = conPointString & item.ConPointID.ToString() & ";"
                        End If
                    Next
                End If
            End If
            If (conPointString.Length > 0) Then
                If (conPointString.LastIndexOf(";") = conPointString.Length - 1) Then
                    conPointString = conPointString.Substring(0, conPointString.Length - 1)
                End If
                Dim arrConPoint As String() = conPointString.Split(Convert.ToChar(";"))
                Dim conPointList As New ListItemCollection
                Dim conPoints As ConPointCollection = ConPointService.GetConPointsListForIDS()
                For i As Integer = 0 To arrConPoint.Length - 1
                    For Each item As ConPoint In conPoints
                        If (arrConPoint(i) = item.ConPointID.ToString() AndAlso conPointList.FindByValue(item.ConPointID.ToString()) Is Nothing) Then
                            conPointList.Add(New ListItem(item.Description, item.ConPointID.ToString()))
                        End If
                    Next
                Next
                ddlConPoint.DataSource = conPointList
                ddlConPoint.DataBind()

                If Not (conPointList.FindByValue(ShoppingConext.ShopingSelectedConsolidation.ToString()) Is Nothing) Then
                    ddlConPoint.SelectedValue = ShoppingConext.ShopingSelectedConsolidation.ToString()
                End If
            End If
        End If
    End Sub

    Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click

        Dim conPointId As Integer = 0
        Try
            conPointId = Convert.ToInt32(ddlConPoint.SelectedValue)
        Catch
            conPointId = 0
        End Try
        'Load the exising saved list for selected Con Point
        If (conPointId > 0) Then
            Dim shoppingItemsList As ShoppingListCollection = ShoppingListService.GetShoppingListByShoppingUserIdAndConPoint(ShoppingConext.ShopingUserId, conPointId)
            Dim previousShoppingList As New Collection
            If (Not shoppingItemsList Is Nothing AndAlso shoppingItemsList.Count > 0) Then
                Dim item As New shoppingItem
                For Each shoppingItem As ShoppingList In shoppingItemsList
                    item = New shoppingItem
                    With item
                        .ItemNo = shoppingItem.IDSSort
                        .ProductCode = shoppingItem.ProductCode
                        .Description = shoppingItem.Description
                        .Quantity = shoppingItem.Qty
                        .UnitDesc = shoppingItem.UnitSetting
                        Try
                            .UnitSettingID = shoppingItem.UnitSettingID
                        Catch ex As Exception
                        End Try

                        .UnitPrice = shoppingItem.UnitPrice
                        .Total = shoppingItem.TotalPrice
                        .UnitWeight = Convert.ToDecimal(shoppingItem.UnitWeight)
                        .Priority = shoppingItem.PriorityIndex
                        .BulkOrderId = shoppingItem.BulkOrderId
                        .TerritoryID = shoppingItem.TerritoryID
                        .MinQtyVolume = Convert.ToDecimal(shoppingItem.MinQtyVolume)
                        .CountryId = conPointId
                    End With
                    previousShoppingList.Add(item)
                Next
                ShoppingConext.ShopingSelectedConsolidation = conPointId
                Session(Constant.SS_SHOPPING_LIST) = previousShoppingList

                Dim shoppingOrderCol As ShoppingOrderCollection = ShoppingListService.GetShoppingOrderByShoppingUserId(ShoppingConext.ShopingUserId)
                If (Not shoppingOrderCol Is Nothing AndAlso shoppingOrderCol.Count > 0) Then
                    For Each shoppingOrderItem As ShoppingOrder In shoppingOrderCol
                        If (ShoppingConext.ShopingSelectedConsolidation = shoppingOrderItem.ConPointID AndAlso ShoppingConext.ShopingSelectedConsolidation > 0) Then
                            If (Not shoppingOrderItem Is Nothing) Then
                                Session("txtQty20Cont") = shoppingOrderItem.Qty20
                                Session("txtQty40Cont") = shoppingOrderItem.Qty40
                                Session("txtQty40HiCube") = shoppingOrderItem.Qty40HC

                                Session("txtOrderNo") = shoppingOrderItem.OrderNo
                                Session("txtHalfPallet") = shoppingOrderItem.HalfPalletSpaces
                                Session("txtExistingOrderVolume") = shoppingOrderItem.FilledVolume

                                Session("rdPartOrder") = shoppingOrderItem.OrderType

                                Session("ddlCountry") = shoppingOrderItem.DestinationCountryId
                                ShoppingConext.ShoppingOrderId = shoppingOrderItem.ShoppingOrderId
                                ShoppingConext.ShopingSelectedConsolidation = shoppingOrderItem.ConPointID
                            End If
                            Exit For
                        End If
                    Next
                End If

                Dim strScript As String = "<script language='javascript'>windowCloseAndRefresh1();</script>"   'refresh
                Page.RegisterStartupScript("windowClose", strScript)
            End If
        End If
    End Sub

End Class
